package hu.dobrosi.dinkycam.j2me.forms;

import hu.dobrosi.dinkycam.gwt.client.StaticUtil;
import hu.dobrosi.dinkycam.j2me.DinkycamApp;
import hu.dobrosi.dinkycam.j2me.Labels;
import hu.dobrosi.dinkycam.j2me.util.Preferences;
import hu.dobrosi.util.StringUtil;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordStoreException;

public class SettingsForm extends DinkycamForm implements CommandListener,
		ItemCommandListener {

	public SettingsForm(DinkycamApp app, String title) {
		super(app, title);
		// TODO Auto-generated constructor stub
	}

	private TextField openIdTextField;
	private TextField camSleepTextField;
	private TextField ownerDeviceTextField;
	private TextField gpsDeviceTextField;
	private ChoiceGroup encodingsChoiceGroup;
	private Preferences preferences;
	private Command cmdBtSettings1;
	private Command cmdBtSettings2;
	private Command cmdFindOwnerDevice;
	private Command cmdAdvancedSettings;
	private Command cmdCapture;

	public void buildGUI() {
		super.buildGUI();
		setTitle(Labels.TITLE_SETTINGS.toString());
		openIdTextField = new TextField("OpenId", "", 8, TextField.PASSWORD);
		openIdTextField.setInitialInputMode("LATIN_DIGITS");
		ownerDeviceTextField = new TextField(Labels.YOUR_MOBILE.toString(),
		/* "38E7D837EDDF" */"", 32, TextField.ANY);
		ownerDeviceTextField.setInitialInputMode("LATIN_DIGITS");
		ownerDeviceTextField.addCommand(cmdBtSettings2 = new Command(
				Labels.SEARCHING.toString(), Command.ITEM, 1));
		ownerDeviceTextField.addCommand(cmdFindOwnerDevice = new Command(
				"Find owner", Command.ITEM, 2));
		ownerDeviceTextField.setItemCommandListener(this);
		gpsDeviceTextField = new TextField(Labels.GPS_DEVICE.toString(), "",
				32, TextField.ANY);
		gpsDeviceTextField.setInitialInputMode("LATIN_DIGITS");
		gpsDeviceTextField.addCommand(cmdBtSettings1 = new Command(
				Labels.SEARCHING.toString(), Command.ITEM, 1));
		gpsDeviceTextField.setItemCommandListener(this);

		// TextField debugField = new TextField("d", "", 2024, TextField.ANY);
		// StringUtil.logger = new TextFieldLogger(debugField);
		// append(debugField);

		camSleepTextField = new TextField(Labels.CAMERA_SLEEP_TIME.toString(),
				"-300", 6, TextField.DECIMAL);
		encodingsChoiceGroup = new ChoiceGroup(
				Labels.MEDIA_ENCODING.toString(), Choice.EXCLUSIVE);

		Object[] strs = StringUtil.splitString("width=160&height=120 "
				+ "width=320&height=240 " + "width=640&height=480 "
				+ "width=1280&height=960 " + "width=1600&height=1200", " ");
		for (int i = 0; i < strs.length; i++) {
			encodingsChoiceGroup.append(strs[i].toString(), null);
		}

		append(openIdTextField);
		append(gpsDeviceTextField);
		// FIXME commented while not stable
		append(ownerDeviceTextField);
		append(camSleepTextField);
		append(encodingsChoiceGroup);
		append(new StringItem("version", StaticUtil.VERSION_MIDLET));
		setCommandListener(this);
		preferences = DinkycamApp.getPreferences();
		preferences.loadTextFieldValue(this, openIdTextField);
		preferences.loadTextFieldValue(this, camSleepTextField);
		preferences.loadChoiceGroupValue(this, encodingsChoiceGroup);
		preferences.loadTextFieldValue(this, ownerDeviceTextField);
		preferences.loadTextFieldValue(this, gpsDeviceTextField);
		addCommand(cmdAdvancedSettings = new Command(
				Labels.ADVANCED_SETTINGS.toString(), Command.SCREEN, 2));
		addCommand(cmdCapture = new Command(Labels.START.toString(),
				Command.OK, 1));
	}

	public void commandAction(Command c, Displayable s) {
		preferences.saveTextFieldValue(this, openIdTextField);
		preferences.saveTextFieldValue(this, camSleepTextField);
		preferences.saveChoiceGroupValue(this, encodingsChoiceGroup);
		preferences.saveTextFieldValue(this, ownerDeviceTextField);
		preferences.saveTextFieldValue(this, gpsDeviceTextField);
		try {
			preferences.save();
		} catch (RecordStoreException e) {
			app.showError(this, "commandAction", e);
		}
		if (c == cmdCapture) {
			app.showCapture();
		} else if (c == cmdAdvancedSettings) {
			app.showAdvancedSettings();
		}
		super.commandAction(c, s);
	}

	public void commandAction(Command c, Item item) {
		if (c.equals(cmdBtSettings1)) {
			app.showGpsBluetoothSettings();
		} else if (c.equals(cmdBtSettings2)) {
			app.showOwnerBluetoothSettings();
		} else if (c == cmdFindOwnerDevice) {
			app.findOwnerDevice();
		}
	}

	public TextField getCamSleepTextField() {
		return camSleepTextField;
	}

	public void setCamSleepTextField(TextField camSleepTextField) {
		this.camSleepTextField = camSleepTextField;
	}

	public ChoiceGroup getEncodingsChoiceGroup() {
		return encodingsChoiceGroup;
	}

	public void setEncodingsChoiceGroup(ChoiceGroup encodingsChoiceGroup) {
		this.encodingsChoiceGroup = encodingsChoiceGroup;
	}

	public TextField getOwnerDeviceTextField() {
		return ownerDeviceTextField;
	}

	public void setOwnerDeviceTextField(TextField ownerDeviceTextField) {
		this.ownerDeviceTextField = ownerDeviceTextField;
	}

	public TextField getGpsDeviceTextField() {
		return gpsDeviceTextField;
	}

	public void setGpsDeviceTextField(TextField gpsDeviceTextField) {
		this.gpsDeviceTextField = gpsDeviceTextField;
	}

	/**
	 * @return the openIdTextField
	 */
	public TextField getOpenIdTextField() {
		return openIdTextField;
	}

	/**
	 * @param openIdTextField
	 *            the openIdTextField to set
	 */
	public void setOpenIdTextField(TextField openIdTextField) {
		this.openIdTextField = openIdTextField;
	}
}
